#include "nw_costmode.h"
#include "platform.h"
#include "string.h"
#include "fsapi.h"

static void reload_costmode(char *path, struct t_guncostmode *costmode)
{
  printf("%s is exist,read it\r\n",path);
	
	fs_fd fp;
  fs_open(&fp, path,FS_O_RDWR);
  if(!fs_size(&fp))//文件没有内容
	{
		printf("%s is zero\r\n",path);
	}
  else//文件不为空
  {
		 printf("%s is %d\r\n",path,fs_size(&fp));
		
		 fs_lseek(&fp,0,FS_SEEK_SET);
		 fs_read(&fp,(char *)&costmode->head,sizeof(struct t_down_costmode));
		 uint32 cnt = costmode->head.timenum;
		 costmode->msg = (struct t_costmodemsg *)os_malloc(cnt*sizeof(struct t_costmodemsg));
		 fs_read(&fp,(char *)costmode->msg,cnt*sizeof(struct t_costmodemsg));
	}
	fs_close(&fp);
}
void costmode_init(struct t_cli_costmode *mode)
{
	 struct t_guncostmode *pmain = &mode->costmode_main;
	 struct t_guncostmode *pback = &mode->costmode_back;
	
	fs_mkdir(NW_COSTMODE_DIR);
	
	char tmpfile[50] = {0};
	
	sprintf(tmpfile,"%s/%s%d_%s",NW_COSTMODE_DIR,COSTMODE_BASE,mode->gunno,NAME_MIANCOSTMODE);
	if(!fs_exist(tmpfile))
	{
	  printf("%s is not exist create it\r\n",tmpfile);
		fs_fd fp;
    fs_open(&fp,tmpfile, FS_O_CREAT);
		fs_close(&fp);
	}
	else//文件存在
	{
		 reload_costmode(tmpfile,pmain);
	}
	sprintf(tmpfile,"%s/%s%d_%s",NW_COSTMODE_DIR,COSTMODE_BASE,mode->gunno,NAME_BACKCOSTMODE);
	if(!fs_exist(tmpfile))
	{
	  printf("%s is not exist create it\r\n",tmpfile);
		fs_fd fp;
    fs_open(&fp,tmpfile, FS_O_CREAT);
		fs_close(&fp);
	}
	else//文件存在
	{
		reload_costmode(tmpfile,pback);
	}
}
void costmode_change(struct t_cli_costmode *mode)//每次生成订单都会执行这个
{
	 #if 1
	   uint32 i = 0;
     for(;i<mode->costmode_main.head.timenum;i++)
     {
		    printf("begintime:%d\r\n",(mode->costmode_main.msg+i)->begintime);// pos = gethalfword(data,&(msg+i)->begintime,pos);
	    	printf("flag:%d\r\n",(mode->costmode_main.msg+i)->flag);//pos = getbyte(data,&(msg+i)->flag,pos);
		    printf("elec_tip:%d\r\n",(mode->costmode_main.msg+i)->elec_tip);//pos = getword(data,&(msg+i)->elec_tip,pos);
		    printf("elec_peak:%d\r\n",(mode->costmode_main.msg+i)->elec_peak);//pos = getword(data,&(msg+i)->elec_peak,pos);
		    printf("elec_normal:%d\r\n",(mode->costmode_main.msg+i)->elec_normal);//pos = getword(data,&(msg+i)->elec_normal,pos);
		    printf("elec_valley:%d\r\n",(mode->costmode_main.msg+i)->elec_valley);//pos = getword(data,&(msg+i)->elec_valley,pos);
		    printf("pcs_server:%d\r\n",(mode->costmode_main.msg+i)->pcs_server);//pos = getword(data,&(msg+i)->pcs_server,pos);
		    printf("pcs_position:%d\r\n",(mode->costmode_main.msg+i)->pcs_position);//pos = getword(data,&(msg+i)->pcs_position,pos);
		    printf("pcs_reserve:%d\r\n",(mode->costmode_main.msg+i)->pcs_reserve);//pos = getword(data,&(msg+i)->pcs_reserve,pos);
    }
		printf("\r\n");
#endif
		
	 if(!pal_istime(mode->costmode_back.head.begintime))//生效时间没到达
	 {
		 return ;
	 }
	 //生效时间到达了
	 struct t_guncostmode *pmain = &mode->costmode_main;
	 struct t_guncostmode *pback = &mode->costmode_back;
	 if(pmain->msg)
	 {
		 os_free((uint8 *)pmain->msg);
	 }
	 memcpy((uint8 *)&(pmain->head),(uint8 *)&(pback->head),sizeof(struct t_down_costmode));
	 pmain->msg = pback->msg;
	 
	 memset((uint8 *)pback,0,sizeof(struct t_guncostmode));
	 
	 char tmpfile[50] = {0};
	 sprintf(tmpfile,"%s/%s%d_%s",NW_COSTMODE_DIR,COSTMODE_BASE,mode->gunno,NAME_MIANCOSTMODE);
	 fs_delete(tmpfile);
	 
	 char tmpold[50] = {0};
   sprintf(tmpold,"%s/%s%d_%s",NW_COSTMODE_DIR,COSTMODE_BASE,mode->gunno,NAME_BACKCOSTMODE);
	 fs_rename(tmpold,tmpfile);
	 //每次启动充电的时候检查备用时间是否生效 如果生效 删除主用文件 备用文件改名成主用文件 变量区的主用文件模型信息指针释放 并重新指向备用信息模型信息 然后在复制备用计费模型的头部到主用 就可以清空备用信息变量区了
   
	

}
uint8 costmode_isnotequal(struct t_cli_costmode *code, struct t_down_costmode *head)
{
	 printf("main_costmode:");
	 for(uint32 i = 0;i<8;i++)
	 {
			printf("%02x",*(code->costmode_main.head.costmode_id+i));
	 } 
	 printf("\r\n");
	 
	 printf("back_costmode:");
	 for(uint32 i = 0;i<8;i++)
	 {
			printf("%02x",*(code->costmode_back.head.costmode_id+i));
	 } 
	 printf("\r\n");
	 
	 printf("down_costmode:");
	 for(uint32 i = 0;i<8;i++)
	 {
			printf("%02x",*(head->costmode_id+i));
	 } 
	 printf("\r\n");
	 
	 return (0 != memcmp((const void *)(head->costmode_id),(const void *)(code->costmode_main.head.costmode_id),8)
			     && 0 != memcmp((const void *)(head->costmode_id),(const void *)(code->costmode_back.head.costmode_id),8)
			);//和主用和备用对比
}

uint8 costmode_update(struct t_cli_costmode *cli_mode, struct  t_guncostmode *mode)
{  	
	if(!mode->msg)
	{
		 return 0;
	}

	struct t_guncostmode *pback = &cli_mode->costmode_back;
	
	memcpy((uint8 *)&pback->head,(uint8 *)&mode->head,sizeof( struct t_down_costmode));//先复制头部信息到备份区
	if(pback->msg)
	{
		os_free(pback->msg);
	}
	pback->msg = mode->msg;//备份区的指针指向新的计费模型
	
	char tmpname[50] = {0};
	sprintf(tmpname,"%s/%s%d_%s",NW_COSTMODE_DIR,COSTMODE_BASE,cli_mode->gunno,NAME_BACKCOSTMODE);
	 
	fs_delete(tmpname);
	
  fs_fd fp;
  fs_open(&fp, tmpname, FS_O_CREAT | FS_O_RDWR);
	fs_lseek(&fp,0,FS_SEEK_SET);
	fs_write(&fp,(char *)&pback->head,sizeof(cli_mode->costmode_back.head));
	fs_write(&fp,(char *)pback->msg,pback->head.timenum *sizeof(struct t_costmodemsg));
	fs_close(&fp);
	
	return 0;
}

void get_costmode(struct t_cli_costmode *cli_mode,uint8 *costmode)
{
	memcpy(costmode,cli_mode->costmode_main.head.costmode_id,8);
}

uint32 get_elecpcs(struct t_cli_costmode *cli_mode,uint32 flag)//电费单价
{
  uint32 cnt = cli_mode->costmode_main.head.timenum;
	struct t_costmodemsg *pmsg;
	for(uint32 i = 0;i<cnt;i++)
	{
		 pmsg = (cli_mode->costmode_main.msg+i);
		 if(flag == pmsg->flag)
		 { 
			  break;
		 }
	}
	switch (flag){
	case E_COSTMODE_TIP:
		   return pmsg->elec_tip;
	case E_COSTMODE_PEAK:
		   return pmsg->elec_peak;
	case E_COSTMODE_NORMAL:
		   return pmsg->elec_normal;
	case E_COSTMODE_VALLEY:
		   return pmsg->elec_valley;
	default:
		   return 0;
	}
	
}
uint32 get_sercepcs(struct t_cli_costmode *cli_mode,uint32 flag)//服务费单价
{
  uint32 cnt = cli_mode->costmode_main.head.timenum;
	struct t_costmodemsg *pmsg;
	for(uint32 i = 0;i<cnt;i++)
	{
		 pmsg = (cli_mode->costmode_main.msg+i);
		 if(flag == pmsg->flag)
		 { 
			  return pmsg->pcs_server;
		 }
	}
	return 0;
}
uint32 get_reservepcs(struct t_cli_costmode *cli_mode,uint32 flag)//预约费单价
{
  uint32 cnt = cli_mode->costmode_main.head.timenum;
	struct t_costmodemsg *pmsg;
	for(uint32 i = 0;i<cnt;i++)
	{
		 pmsg = (cli_mode->costmode_main.msg+i);
		 if(flag == pmsg->flag)
		 { 
			  return pmsg->pcs_reserve;
		 }
	}
	return 0;
}
uint32 get_positionpcs(struct t_cli_costmode *cli_mode,uint32 flag)//占位费单价
{
	 uint32 cnt = cli_mode->costmode_main.head.timenum;
	 struct t_costmodemsg *pmsg;
	 for(uint32 i = 0;i<cnt;i++)
	 {
		 pmsg = (cli_mode->costmode_main.msg+i);
		 if(flag == pmsg->flag)
		 { 
			  return pmsg->pcs_position;
		 }
	 }
	 return 0;
}

static uint32 get_total_sec(uint16 time)
{
	uint16 beginmin = (uint16)(time>>8);
  uint16 beginsec = (uint16)(time & 0x0f);
	
  return  beginmin*60 + beginsec;
}
static 	struct t_costmodemsg * gettime(struct t_cli_costmode *cli_mode,uint8 min,uint8 sec)
{
	 struct t_costmodemsg *  pmsg = 0;
	 uint32 cnt = cli_mode->costmode_main.head.timenum;
	 uint16 totalsec = min*60+ sec;
	
   for(uint32 i = 0;i<cnt;i++)
	 {		
		 pmsg = (cli_mode->costmode_main.msg+i);
		 uint16 tmpsec = get_total_sec(pmsg->begintime);
		 if(totalsec>= tmpsec)
		 {
			 if(i+1>=cnt){//字段的最后一项
				  return pmsg;
			 }
			 uint32 nextsec = get_total_sec((pmsg+1)->begintime);
			 if(totalsec <= nextsec){
				  return pmsg;
			 }
		 }
	}
	return 0; 
}
uint32 get_timeflag(struct t_cli_costmode *cli_mode,uint8 min,uint8 sec)
{
 	 struct t_costmodemsg * pmsg = gettime(cli_mode,min,sec);
	 if(!pmsg){
		  return 0;
	 }
	 return pmsg->flag;
	
}
uint32 get_elecpcsbytime(struct t_cli_costmode *cli_mode,uint8 min,uint8 sec)//电费单价
{
	 struct t_costmodemsg * pmsg = gettime(cli_mode,min,sec);
	 if(!pmsg){
		  return 0;
	 }
	 	switch (pmsg->flag){
	case E_COSTMODE_TIP:
		   return pmsg->elec_tip;
	case E_COSTMODE_PEAK:
		   return pmsg->elec_peak;
	case E_COSTMODE_NORMAL:
		   return pmsg->elec_normal;
	case E_COSTMODE_VALLEY:
		   return pmsg->elec_valley;
	default:
		   return 0;
   }
}
uint32 get_servepcsbytime(struct t_cli_costmode *cli_mode,uint8 min,uint8 sec)//服务费单价
{
	 struct t_costmodemsg * pmsg = gettime(cli_mode,min,sec);
	 if(!pmsg){
		  return 0;
	 }
	 return pmsg->pcs_server;
}
uint32 get_reservepcsbytime(struct t_cli_costmode *cli_mode,uint8 min,uint8 sec)//预约费单价
{
	 struct t_costmodemsg * pmsg = gettime(cli_mode,min,sec);
	 if(!pmsg){
		  return 0;
	 }
	 return pmsg->pcs_reserve;
}
uint32 get_positionpcsbytime(struct t_cli_costmode *cli_mode,uint8 min,uint8 sec)//占位费单价
{
	 struct t_costmodemsg * pmsg = gettime(cli_mode,min,sec);
	 if(!pmsg){
		  return 0;
	 }
	 return pmsg->pcs_position;
}		
